from datetime import datetime

from flask import current_app
from models.CommSubSysModels import Article, Help, Comment, Footstep
from models.UserSubSysModels import Expert
from utils.dataBase import db_session


class commService:
    def __init__(self):
        pass

    # 文章列表
    def articlelist(self, type):
        s = db_session.query(Article).filter(Article.type == type).all()
        list = []
        if s == None:
            dict = {}
            dict['status'] = 0
            list.append(dict)
        else:
            for item in s:
                dict = {}
                dict['aid'] = item.aid
                dict['type'] = item.type
                dict['title'] = item.title
                dict['fromid'] = item.fromid
                dict['time'] = item.time
                dict['content'] = item.content
                dict['like'] = item.like
                list.append(dict)
        return list

    # 搜索文章
    def searcharticle(self, str):
        express = "%" + str + "%"
        obj = db_session.query(Article).filter(Article.title.like(express)).all()
        list = []
        if obj == None:
            dict = {}
            dict['status'] = 0
            list.append(dict)
        else:
            for item in obj:
                dict = {}
                dict['aid'] = item.aid
                dict['type'] = item.type
                dict['title'] = item.title
                dict['fromid'] = item.fromid
                dict['time'] = item.time
                dict['content'] = item.content
                dict['like'] = item.like
                list.append(dict)
        return list

    # 文章详情
    def articledetails(self, uid, aid):
        obj = db_session.query(Article).filter(Article.aid == aid).first()
        list = []
        # 获取现在的时间
        now_time = datetime.now()
        timestr = datetime.strftime(now_time, '%Y/%m/%d %H:%M:%S')
        if obj == None:
            dict = {}
            dict['status'] = 0
            list.append(dict)
        else:
            dict = {}
            dict['aid'] = obj.aid
            dict['type'] = obj.type
            dict['title'] = obj.title
            dict['fromid'] = obj.fromid
            dict['time'] = obj.time
            dict['content'] = obj.content
            dict['like'] = obj.like
            list.append(dict)
            # 写入用户足迹
            footstep = Footstep(uid=uid, aid=aid, time=timestr)
            db_session.add(footstep)
            try:
                db_session.commit()
            except Exception as r:
                current_app.logger.info(r)
            current_app.logger.info('{}的用户浏览了{}的文章'.format(uid, aid))
        return list

    # 点赞文章
    def like(self, aid):
        re = db_session.query(Article).filter(Article.aid == aid).first()
        list = []
        if re == None:
            dict = {}
            dict['status'] = 0
            list.append(dict)
            return list
        else:
            likes = re.like + 1

        u = db_session.query(Article).filter(Article.aid == aid).update({"like": likes})
        if u == 1:
            current_app.logger.info('id为{}的文章点赞成功'.format(aid))
        try:
            db_session.commit()
            dict = {}
            dict['status'] = 1
            list.append(dict)
            return list
        except Exception as r:
            dict = {}
            dict['status'] = 0
            list.append(dict)
            return list

    # 评论文章
    def comment(self, type, id, uid, content):
        # 获取现在的时间
        now_time = datetime.now()
        timestr = datetime.strftime(now_time, '%Y/%m/%d %H:%M:%S')
        list = []
        dict = {}
        if type is '1' or type is '2':
            comment = Comment(replyid=id, uid=uid, content=content, time=timestr, type=type)
            db_session.add(comment)
            try:
                db_session.commit()
                dict['status'] = 'success'
            except Exception as r:
                current_app.logger.info(r)
                dict['status'] = 'fail'
        else:
            dict['status'] = '该类型不存在'
        list.append(dict)
        return list

    # 求助列表
    def helplist(self):
        obj = db_session.query(Help).filter(Help.toid == 0).all()
        list = []
        if obj == None:
            dict = {}
            dict['status'] = 0
            list.append(dict)
        else:
            for item in obj:
                dict = {}
                dict['hid'] = item.hid
                dict['title'] = item.title
                dict['content'] = item.content
                dict['time'] = item.time
                dict['fromid'] = item.fromid
                dict['toid'] = item.toid
                list.append(dict)
        return list

    # 指定求助列表
    def helpforlist(self, eid):
        list = []
        # 查询是否有这个专家
        re = db_session.query(Expert).filter(Expert.eid == eid).first()
        if re is not None:
            # 选择出向该专家提问的help
            obj = db_session.query(Help).filter(Help.toid == eid).all()
            if not obj:
                dict = {}
                dict['message'] = '目前无定向求助'
                list.append(dict)
            else:
                for item in obj:
                    dict = {}
                    dict['hid'] = item.hid
                    dict['title'] = item.title
                    dict['content'] = item.content
                    dict['time'] = item.time
                    dict['fromid'] = item.fromid
                    dict['toid'] = item.toid
                    list.append(dict)
        else:
            dict = {}
            dict['message'] = '无该专家'
            list.append(dict)
        return list

    # 求助详情
    def helpdetails(self, hid):
        list = []
        obj = db_session.query(Help).filter(Help.hid == hid).first()
        if obj is None:
            dict = {}
            dict['message'] = '找不到该求助id'
            list.append(dict)
        else:
            dict = {}
            dict['hid'] = obj.hid
            dict['title'] = obj.title
            dict['content'] = obj.content
            dict['time'] = obj.time
            dict['fromid'] = obj.fromid
            dict['toid'] = obj.toid
            list.append(dict)
        return list

    # 发布求助
    def addhelp(self, title, content, fromid):
        now_time = datetime.now()
        timestr = datetime.strftime(now_time, '%Y/%m/%d %H:%M:%S')
        list = []
        dict = {}
        help = Help(title=title, content=content, time=timestr, fromid=fromid, toid=0)
        db_session.add(help)
        try:
            db_session.commit()
        except Exception as r:
            current_app.logger.info(r)
        dict['status'] = '成功发布求助'
        list.append(dict)
        return list

    # 发布定向求助
    def addhelpfor(self, title, content, fromid, toid):
        now_time = datetime.now()
        timestr = datetime.strftime(now_time, '%Y/%m/%d %H:%M:%S')
        list = []
        dict = {}
        help = Help(title=title, content=content, time=timestr, fromid=fromid, toid=toid)
        db_session.add(help)
        try:
            db_session.commit()
        except Exception as r:
            current_app.logger.info(r)
        dict['status'] = '成功向{}发起定向求助'.format(toid)
        list.append(dict)
        return list

    # 精品区
    def top(self, type):
        obj = db_session.query(Article).filter(Article.type == type).order_by(Article.like.desc()).all()
        list = []
        if obj is None:
            dict = {}
            dict['message'] = '暂时没有文章'
            list.append(dict)
        else:
            for item in obj:
                dict = {}
                dict['aid'] = item.aid
                dict['type'] = item.type
                dict['title'] = item.title
                dict['fromid'] = item.fromid
                dict['time'] = item.time
                dict['content'] = item.content
                dict['like'] = item.like
                list.append(dict)
        return list

    # 足迹
    def footstep(self, uid):
        obj = db_session.query(Footstep).filter(Footstep.uid == uid).order_by(Footstep.time.desc()).all()
        list = []
        if obj is None:
            dict = {}
            dict['message'] = '该用户足迹为空'
            list.append(dict)
        else:
            for item in obj:
                dict = {}
                dict['time'] = item.time
                dict['title'] = item.article.title
                dict['content'] = item.article.content
                list.append(dict)
        return list
